/*
 * @Author: C.
 * @Date: 2023-02-03 09:37:33
 */
import { isNullOrUnDef, isArray } from '/@/utils/is';
import { projectSettings } from '../../settings/projectSettings';
import { TableImg } from '/@/components/Table';
import { unref } from 'vue';
import TableLink from '/~/components/TableLink/src/TableLink.vue';
import { stateEnum } from '/@/maps/index';
import { Tag } from 'ant-design-vue';
import { getImgUrl } from '/~/utils/system/index';
import { Icon } from '/@/components/Icon';

// { text, record, index, column, type }
interface ITransform {
  text: string;
  record?: Recordable;
  index?: number;
  column?: Recordable;
  type?: 'string' | 'default';
}

export function handleEmpty({ text }: ITransform) {
  return isNullOrUnDef(text) || text === '' ? projectSettings.dataPlaceholder : text;
}
export function amountFormat({ text }: ITransform) {
  if (isNullOrUnDef(text)) return projectSettings.dataPlaceholder;
  return String(Number(text).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}

export function handleImg({ text, column, type }: ITransform) {
  let imgList: string[] = [];
  if (isArray(text)) {
    imgList = text.map((item) => getImgUrl(item));
  } else {
    imgList = [getImgUrl(text)];
  }
  const circled = column?.componentProps?.circled ?? false;
  if (type == 'string') return text;
  return <TableImg imgList={imgList} simpleShow={true} size={60} circled={circled}></TableImg>;
}

export function handleLink({ text, record, column, type }: ITransform) {
  if (!(text && record && column && column.componentProps)) return text;
  const { routerMethod = 'query', routerName, routeKeyValue } = column.componentProps;
  const obj = {};
  routeKeyValue.forEach((arr) => {
    obj[arr[0]] = unref(record[arr[1]]);
  });
  const params = {
    name: routerName,
    [routerMethod]: obj,
  };
  if (type == 'string') return text;
  return <TableLink {...params} text={text}></TableLink>;
}

// 首字母大写
export function capitalizeFirstLetter(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}
// export function capitalizeFirstLetter(str: string) {
//   const res = str.replace(/\//g, '');
//   return res.charAt(0).toUpperCase() + res.slice(1);
// }

export function handleState({ text, type }: ITransform) {
  const stateItem = stateEnum.getItem(text);
  if (!stateItem) return projectSettings.dataPlaceholder;
  if (type == 'string') return stateItem?.label;
  return (
    <Tag color={stateItem?.color}>
      {/* {icon:()=>{return stateItem?.icon && <Icon icon={stateItem?.icon}></Icon>}} */}
      {{
        default: () => stateItem.label,
        icon: () =>
          stateItem?.icon && <Icon icon={stateItem?.icon} class={'pt-0.5'} size={14}></Icon>,
      }}
    </Tag>
  );
}

export function handleTag({ text, column, type }) {
  let displayText: string = text;
  let color;
  if ((column.edit && column?.editComponentProps?.options) ?? false) {
    const options = column.editComponentProps.options;
    for (const item of options) {
      if (item.value === text) {
        displayText = item.label;
        color = item.color;
        break;
      }
    }
  }
  if (type == 'string') return displayText;
  return <Tag color={color}>{handleEmpty({ text: displayText })}</Tag>;
}

export function handleBoolean({ text, type }) {
  const displayText = Boolean(text) ? '是' : '否';
  if (type == 'string') return displayText;
  return <Tag color={Boolean(text) ? 'green' : 'grey'}>{displayText}</Tag>;
}
